import time

from sam.util.fileUtil2 import write_file_quick, read_file_quick
from sam.util.httpUtil2 import BaseBrowser

RICH_TEXT_ELEMENT_LIST = ["p", "ul", "blockquote", "h2"]


class DouBan(BaseBrowser):
    def __init__(self):
        super().__init__()
        self.login()

    def login(self):
        self.browser.maximize_window()
        self.browser.get("https://accounts.douban.com/passport/login?source=main")
        time.sleep(20)
        print("登录OK")

    def topic_a_url(self, topic_url):
        self.browser.get(topic_url)
        # 手动 划取所有的评论链接
        time.sleep(10)
        a_element_list = self.browser.find_elements_by_css_selector("a.comments-count")
        a_url_list = []
        for a_element in a_element_list:
            a_url = a_element.get_attribute("href")
            a_url_list.append(a_url)

        write_file_quick(data_list=a_url_list, export_file_name=f"第一次来月经的时候", optional="cover")
        print("ok")

    def cmoment_text(self, cmoment_url):
        self.browser.get(cmoment_url)
        article_element_list = self.browser.find_elements_by_css_selector("blockquote")
        note_element_list = self.browser.find_elements_by_css_selector("note")
        if article_element_list:
            article_element = self.browser.find_element_by_css_selector("blockquote")
            article = article_element.text
        elif note_element_list:
            note_element = self.browser.find_element_by_css_selector("note")
            article = note_element.text
        else:
            article = cmoment_url

        comment_element_list = self.browser.find_elements_by_css_selector("div.comment-content")
        if comment_element_list:
            comment_list = [comment_element.text for comment_element in comment_element_list]
            comment_list_str = " ".join(comment_list)
        else:
            comment_list_str = ""
        return article, comment_list_str

    def main(self):
        url_list = read_file_quick(file_type="csv")
        for url in url_list:
            article, comment = self.cmoment_text(url)
            write_file_quick(data_list=[article], export_file_name=f"第一次来月经的时候-话题", optional="append", file_type="txt")
            write_file_quick(data_list=[comment], export_file_name=f"第一次来月经的时候-评论", optional="append", file_type="txt")
            print(f"{url}  抓取完了")


def dou_ban_question():
    process = DouBan()
    process.main()
    # process.topic_a_url(topic_url=f"https://www.douban.com/gallery/topic/158927/")
    # article, comment_list_str = process.cmoment_text(
    #     cmoment_url=f"https://www.douban.com/people/161746051/status/3216170151/#comments")
    # pass


if __name__ == "__main__":
    dou_ban_question()
